import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import "./index.less";
import { eqpInspectsApi } from "@/api/erp/equipments/inspection";
import { EquipmentsApi } from "@/api/erp/equipments/device";
import { getListSimpleUsers } from "@/api/system/user";
let reload: Function;
let inspId: string;
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	const en_vi = "drugStorage.equipment.inspection";
	const formSchema: FormSchema[] = [
		{
			label: t(`${en_vi}.equipmentName`),
			field: "eqpId",
			component: "ApiSelect",
			componentProps: {
				api: EquipmentsApi.getEquipmentsPage,
				labelField: "eqpName",
				valueField: "eqpId"
			}
		},
		{
			label: t(`${en_vi}.inspectionTime`),
			field: "inspDt",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.inspectorName`),
			field: "inspectorId",
			component: "ApiSelect",
			componentProps: {
				api: getListSimpleUsers,
				labelField: "nickname",
				valueField: "id"
			}
		},
		// {
		// 	label: "检查类型",
		// 	field: "inspType",
		// 	component: "Select",
		// 	componentProps: {
		// 		options: [
		// 			{
		// 				label: "每日",
		// 				value: "0"
		// 			},
		// 			{
		// 				label: "每周",
		// 				value: "1"
		// 			},
		// 			{
		// 				label: "每月",
		// 				value: "2"
		// 			},
		// 			{
		// 				label: "每季度",
		// 				value: "3"
		// 			},
		// 			{
		// 				label: "每年",
		// 				value: "4"
		// 			}
		// 		]
		// 	}
		// },
		{
			label: t(`${en_vi}.inspectionResult`),
			field: "inspResult",
			component: "Select",
			componentProps: {
				options: [
					{
						label: t(`${en_vi}.qualified`),
						value: "0"
					},
					{
						label: t(`${en_vi}.unqualified`),
						value: "1"
					},
					{
						label: t(`${en_vi}.requiresRepair`),
						value: "2"
					}
				]
			}
		},
		{
			label: t(`${en_vi}.description`),
			field: "inspDesc",
			component: "InputTextArea"
		},
		{
			label: t(`erp.warehouses.status`),
			field: "status",
			component: "RadioGroup",
			componentProps: {
				options: [
					{
						label: t("erp.warehouseGroupType.open"),
						value: "0"
					},
					{
						label: t("erp.warehouseGroupType.close"),
						value: "1"
					}
				]
			}
		}
	];

	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, async data => {
		reload = data.reload;
		await resetFields();
		if (data.record) {
			console.log(data.record);
			inspId = data.record.inspId;
			setFieldsValue(data.record);
		}
	});

	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 100,
		baseColProps: {
			span: 24
		},
		initialValues: {
			status: "0"
		}
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		let api = eqpInspectsApi.createeqpInspects;
		if (inspId) {
			api = eqpInspectsApi.updateeqpInspects;
		}
		const res = await api({ ...formValue, inspId });
		if (res) {
			inspId ? message.success(t("kevin_ui.updateSuccess")) : message.success(t("kevin_ui.addedSuccessfully"));
			reload();
			closeDrawer();
			inspId = "";
		}
	};
	return (
		<BasicDrawer showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<div className="warehouses">
				<BasicForm register={register}></BasicForm>
			</div>
		</BasicDrawer>
	);
};
export default index;
